﻿// TestClient.DraggedFileView.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DraggedFileView.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/16/2011
// ----------------------------------------------------------------------------
namespace TestClient
{
	#region Using Directives
	using System.IO;
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Media.Imaging;
	using System;

	#endregion

	#region DraggedFileView
	/// <summary>
	/// DraggedFileView for the Drag Drop Demo.
	/// </summary>
	public class DraggedFileView : Control
	{
		#region Fields
		internal FileInfo file;
		private MainPageViewModel mainPage;
		private Button btnTag;
		private Button btnComment;
		private Button btnRemove;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="fileInfo">The <see cref="FileInfo"/>.</param>
		public DraggedFileView(FileInfo fileInfo, WriteableBitmap image, MainPageViewModel mainPageViewModel)
		{
			this.mainPage = mainPageViewModel;
			this.file = fileInfo;
			this.FileName = fileInfo.Name;
			this.Thumbnail = image;
			this.DefaultStyleKey = typeof(DraggedFileView);
			this.Visibility = System.Windows.Visibility.Collapsed;
		}
		#endregion

		#region Properties

		#region FileName
		public string FileName
		{
			get { return (string)GetValue(FileNameProperty); }
			set { SetValue(FileNameProperty, value); }
		}
		public static readonly DependencyProperty FileNameProperty =
			DependencyProperty.Register("FileName", typeof(string), typeof(DraggedFileView), new PropertyMetadata(string.Empty));
		#endregion

		#region FileImage
		/// <summary>
		/// Gets or sets the Thumbnail image.
		/// </summary>
		public WriteableBitmap Thumbnail
		{
			get { return (WriteableBitmap)GetValue(ThumbnailProperty); }
			internal set { SetValue(ThumbnailProperty, value); }
		}
		public static readonly DependencyProperty ThumbnailProperty =
			DependencyProperty.Register("Thumbnail", typeof(WriteableBitmap), typeof(DraggedFileView), null);
		#endregion

		#region FileComment
		/// <summary>
		/// Gets or sets the FileComment value.
		/// </summary>
		public string FileComment
		{
			get { return (string)GetValue(FileCommentProperty); }
			set { SetValue(FileCommentProperty, value); }
		}
		/// <summary>
		/// FileCommentProperty
		/// </summary>
		public static readonly DependencyProperty FileCommentProperty =
			DependencyProperty.Register("FileComment", typeof(string), typeof(DraggedFileView), new PropertyMetadata(string.Empty));
		#endregion

		#region FileTag
		/// <summary>
		/// Gets or sets the FileTag value.
		/// </summary>
		public string FileTag
		{
			get { return (string)GetValue(FileTagProperty); }
			set { SetValue(FileTagProperty, value); }
		}
		/// <summary>
		/// FileTagProperty
		/// </summary>
		public static readonly DependencyProperty FileTagProperty =
			DependencyProperty.Register("FileTag", typeof(string), typeof(DraggedFileView), new PropertyMetadata(string.Empty));
		#endregion

		#endregion

		#region Overrides
		/// <summary>
		/// Apply the template.
		/// </summary>
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			if (null != this.btnComment)
			{
				this.btnComment.Click -= new RoutedEventHandler(btnComment_Click);
			}
			this.btnComment = this.GetTemplateChild("btnComment") as Button;
			if (null != this.btnComment)
			{
				this.btnComment.Click += new RoutedEventHandler(btnComment_Click);
			}

			if (null != this.btnRemove)
			{
				this.btnRemove.Click -= new RoutedEventHandler(btnRemove_Click);
			}
			this.btnRemove = this.GetTemplateChild("btnRemove") as Button;
			if (null != this.btnRemove)
			{
				this.btnRemove.Click += new RoutedEventHandler(btnRemove_Click);
			}

			if (null != this.btnTag)
			{
				this.btnTag.Click -= new RoutedEventHandler(btnTag_Click);
			}
			this.btnTag = this.GetTemplateChild("btnTag") as Button;
			if (null != this.btnTag)
			{
				this.btnTag.Click += new RoutedEventHandler(btnTag_Click);
			}

		}
		#endregion

		#region Methods
		/// <summary>
		/// Creates a new DraggedFileView
		/// </summary>
		/// <param name="fileInfo">The fileInfo of the file.</param>
		/// <param name="image">The WriteableBitmap that contains the image.</param>
		/// <param name="mainPageViewModel">The MainPage's ViewModel.</param>
		/// <returns>The newly created DraggedFileView.</returns>
		public static DraggedFileView Create(FileInfo fileInfo, WriteableBitmap image, MainPageViewModel mainPageViewModel)
		{
			return new DraggedFileView(fileInfo, image, mainPageViewModel);
		}
		#endregion

		#region Event Handlers
		void btnRemove_Click(object sender, RoutedEventArgs e)
		{
			this.mainPage.Files.Remove(this);
		}
		void btnTag_Click(object sender, RoutedEventArgs e)
		{
			TagFile tag = new TagFile();
			tag.Closed += new System.EventHandler(tag_Closed);
			if (!string.IsNullOrEmpty(this.FileTag))
				tag.TagText = this.FileTag;
			tag.Show();
		}
		void tag_Closed(object sender, System.EventArgs e)
		{
			TagFile tag = sender as TagFile;
			this.FileTag = tag.TagText;
		}
		void btnComment_Click(object sender, RoutedEventArgs e)
		{
			CommentFile comment = new CommentFile();
			comment.Closed += new System.EventHandler(comment_Closed);
			if (!string.IsNullOrEmpty(this.FileComment))
				comment.CommentText = this.FileComment;
			comment.Show();
		}
		void comment_Closed(object sender, System.EventArgs e)
		{
			CommentFile comment = sender as CommentFile;
			this.FileComment = comment.CommentText;
		}
		#endregion
	}
	#endregion
}